<?

include("sqlconnect.php");

session_start();
$curuser = $_SESSION['usrid'];

if(isset($_POST['ld']))
$parameter = $_POST['ld'];
else echo 'Ajax Error';


if($parameter == 'listallfriends')
{
	$query= "SELECT * FROM freinds_list where user_id = '".$curuser."'";
	$result= mysql_query($query) or die(mysql_error());
	$friends_count = mysql_num_rows($result);
	echo '<div id="friendcount'.$curuser.'" class ="friendbox">All friends - ( '.$friends_count.' )</div><br><br><br>';
	$friends_list[] = array();
	$i=0;
	while($row = mysql_fetch_object($result))
	{
		$usrid_f= $row->user_id_2; 
		$friends_list[$i] = $usrid_f;
		$query_f= "SELECT * FROM users where user_id = '".$usrid_f."'";
		$result_f= mysql_query($query_f) or die(mysql_error());
		$row_f = mysql_fetch_object($result_f);
		$f_fname = $row_f->user_fname;
		$f_mailid = $row_f->email_id;
		$f_gender = $row_f->gender;
		
		echo ' 
		<div id="f1'.$friends_list[$i].'" class ="friendbox">
		<div id="u'.$friends_list[$i].'" onclick="viewprofile(this.id)" class ="namedisplay">'.$f_fname.' <br><br></div>
		<div id="fmailid'.$friends_list[$i].'" class="mailiddisplay">'.$f_mailid.' </div>
		<div id="fgender" class="fgenderdisplay">                  '.$f_gender.'</div>
		</div> ';
		$i++;
	}

}


if($parameter == 'loadmyprofile')
{

	$query= "SELECT * FROM users where user_id = '".$curuser."'";
	$result= mysql_query($query) or die(mysql_error());	
	$row = mysql_fetch_object($result);
	
	$my_fname = $row->user_fname;
	$my_lname = $row->user_lname;
	$my_dob = $row->user_dob;
	$my_gender = $row->gender;
	$my_mailid = $row->email_id;
	$my_sec_q = $row->sec_q;
	$my_sec_a = $row->sec_a;

	echo '<div id="myprofile" class="box">         
      <h1>'.$my_fname.' '.$my_lname.'</h1><br>
      <form action="updateprofilerecord.php" method="post" name="updateform">
      <label class ="rowlabel"> 
      <span class="rowleft"> Email ID: </span>
      <input name="email_id" type="text" class="input_text" readonly="true"   value="'.$my_mailid.'" /><br /> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">Date of Birth: </span>
      <input name="user_dob" type="text" class="input_text" readonly="true"   value="'.$my_dob.'" /> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">First Name: </span>
      <input name="fname" type="text" class="input_text" readonly="true" value="'.$my_fname.'"/><br /> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">Last Name: </span>
      <input name="lname" type="text" class="input_text" readonly="true"  value="'.$my_lname.'"/> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">Security Question:</span> 
      <input name="sec_q" type="text"  class="input_text"  readonly="true" size="50" value="'.$my_sec_q.'"/> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">Security Answer: </span>
      <input name="sec_a" type="text" class="input_text" readonly="true" value="'.$my_sec_a.'" /> 
      </label>
      <input type="submit" name="editsubmit" id="button" class ="decbutton" value="Submit for Update">
      </form>
  	  </div>';
	 			
} 
  

if($parameter == 'freindstoadd')
{
	
$query= "SELECT * FROM freinds_list where user_id = '".$curuser."'";
$result= mysql_query($query) or die(mysql_error());

$i=0;
while($row = mysql_fetch_object($result))
	{
		$usrid_f= $row->user_id_2; 
		$friends_list[$i] = $usrid_f;
		$query_f= "SELECT * FROM users where user_id = '".$usrid_f."'";
		$result_f= mysql_query($query_f) or die(mysql_error());
		$row_f = mysql_fetch_object($result_f);
		$f_fname = $row_f->user_fname;
		
		echo '<div id="addgrpfriend'.$friends_list[$i].'" class ="friendbox">
            
   			<div id="grpfriendname'.$friends_list[$i].'"> '.$f_fname.'</div>

 			<input name="" type="checkbox" id="grpfriendtick'.$friends_list[$i].'" value="Add Him" class ="fgenderdisplay" 	onchange="checkgrpfriend(this.id)"/>
 
			</div><br>';
		$i++;
	}

}

if($parameter == 'addthisfriend')
{
		
		$frd = $_POST['fr'];
		$query1 = "INSERT INTO  freinds_list(user_id,user_id_2)
		VALUES ('$curuser','$frd')";

		$result1= mysql_query($query1) or die(mysql_error());
		
}



if($parameter == 'viewprofile')
{
	
	//if(isset($_POST['fid']))
	$frid = $_POST['vf'];

	//$vfname = $_POST['vf'];
	$query= "SELECT * FROM users where user_id = '".$frid."'";
	$result= mysql_query($query) or die(mysql_error());	
	$row = mysql_fetch_object($result);
	
	$frid = $row->user_id;
	$my_fname = $row->user_fname;
	$my_lname = $row->user_lname;
	$my_dob = $row->user_dob;
	$my_gender = $row->gender;
	$my_mailid = $row->email_id;

	
	$query= "SELECT * FROM freinds_list where user_id = '".$curuser."'";
	$result= mysql_query($query) or die(mysql_error());	
	while($row = mysql_fetch_object($result))
	{
		$temp = $row->user_id_2;

		if($temp == $frid)
		{
			$f_test = 1;			
			break;
		}else{
			
				$f_test = 0;
			}

	}

	if($f_test == 1)
	{
			echo '<div id="fprofile" class="box">         
	      <h1>'.$my_fname.' '.$my_lname.'</h1><br>
	      <form action="" method="post" name="updateform">

	      <label class ="rowlabel"> 
	      <span class="rowleft"> Email ID: </span>
	      <input name="email_id" type="text" class="input_text" readonly="true"   value="'.$my_mailid.'" /><br /> 
	      </label>
	      <label class ="rowlabel"> 
	      <span class="rowleft">Date of Birth: </span>
	      <input name="user_dob" type="text" class="input_text" readonly="true"   value="'.$my_dob.'" /> 
	      </label>
	      <label class ="rowlabel"> 
	      <span class="rowleft">First Name: </span>
	      <input name="fname" type="text" class="input_text" readonly="true" value="'.$my_fname.'"/><br /> 
	      </label>
	      <label class ="rowlabel"> 
	      <span class="rowleft">Last Name: </span>
	      <input name="lname" type="text" class="input_text" readonly="true"  value="'.$my_lname.'"/> 
	      </label>
	      </form>
	  	  </div>';
  	}else {
  		
  	  echo '<div id="fprofile" class="box">         
      <h1>'.$my_fname.' '.$my_lname.'</h1>
      
      <a class="addbuddy" id="addfriend" alt="'.$frid.'"> </a><br>
      <form action="" method="post" name="updateform">
      <label class ="rowlabel"> 
      <span class="rowleft"> Email ID: </span>
      <input name="email_id" type="text" class="input_text" readonly="true"   value="'.$my_mailid.'" /><br /> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">Date of Birth: </span>
      <input name="user_dob" type="text" class="input_text" readonly="true"   value="'.$my_dob.'" /> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">First Name: </span>
      <input name="fname" type="text" class="input_text" readonly="true" value="'.$my_fname.'"/><br /> 
      </label>
      <label class ="rowlabel"> 
      <span class="rowleft">Last Name: </span>
      <input name="lname" type="text" class="input_text" readonly="true"  value="'.$my_lname.'"/> 
      </label>
      <label class ="rowlabel"> 
           
      </form>
  	  </div>';

  			}	

}



?>